package pt.fct.mpc.lastfm.artist;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class ArtistXmlHandler extends DefaultHandler{

	private ArtistData artistDataSet = new ArtistData();
	private String position = "";

	public ArtistData getParsedData() {
		return this.artistDataSet;
	}

	public void startDocument() throws SAXException {
		this.artistDataSet = new ArtistData();
	}

	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/** Gets be called on opening tags like:
	 * <tag>
	 * Can provide attribute(s), when xml was like:
	 * <tag attribute="attributeValue">*/
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		position = position.concat("." + localName);
		if (localName.equals("image")) {
			position = position.concat("." + atts.getValue("size")); 
		}
	}

	/** Gets be called on closing tags like:
	 * </tag> */
	public void endElement(String namespaceURI, String localName, String qName)
	throws SAXException {
		position = position.substring(0, position.lastIndexOf("." + localName));
	}

	/** Gets be called on the following structure:
	 * <tag>characters</tag> */
	public void characters(char ch[], int start, int length) {
			if(position.compareTo(".lfm.artist.url")==0){
			artistDataSet.setUrl(new String(ch, start, length));
		} else if(position.compareTo(".lfm.artist.image.small")==0){
			artistDataSet.setImage_small(new String(ch, start, length));
		} else if(position.compareTo(".lfm.artist.image.medium")==0){
			artistDataSet.setImage_medium(new String(ch, start, length));
		} else if(position.compareTo(".lfm.artist.image.large")==0){
			artistDataSet.setImage_large(new String(ch, start, length));
		} else if(position.compareTo(".lfm.artist.image.extralarge")==0){
			artistDataSet.setImage_extraLarge(new String(ch, start, length));
		} else if(position.compareTo(".lfm.artist.image.mega")==0){
			artistDataSet.setImage_mega(new String(ch, start, length));
		} else if(position.compareTo(".lfm.artist.tags.tag.name")==0){
			artistDataSet.addTag(new String(ch, start, length));
		} else if(position.compareTo(".lfm.artist.bio.summary")==0){
			artistDataSet.setBio_summary(new String(ch, start, length));
		} else if(position.compareTo(".lfm.artist.bio.content")==0){
			artistDataSet.setBio_content(new String(ch, start, length));
		} 
	}
}